<?php

namespace App\Http\Controllers\Admin;

use App\Model\Branch;
use App\Model\Order;
use App\Model\Order1;
use App\Model\OrderError;
use App\Model\PushOrder;
use App\Model\tel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CallBackController extends Controller
{
    //正式
    private $url = "http://openapi.fulu.com/api/getway";

    //品盛
//    private $app_key = "YZ2Fu5Y86kfnF4qIcKCtOK9PjJzlv4w7kq+bPPu0SmeZxjqn5gUBgTAthCnxFOXP";
//    private $app_secret = "10888069844749068c5f6fe742796e0b";

    //普兴
//    private $app_key = "k62nIJvlYq9lhEQfrmC1dWhavZFQCsJNX/iC9ivhTnmwjppWUrl28EjHts2tGTZg";
//    private $app_secret = "b6888a54dfa143ab9f69f035b831d60d";

    //竹清
    private $app_key = "5aN/mSAIt3TKzydo+CETbqGduinUqPfksmCm60034GyF2S8su68+au0Dr3uJa31F";
    private $app_secret = "2f887bdc2f5b4151b8b06d52a628a52b";

    //群秋话费充值回调
    public function qq_AutoConfirmOrder(Request $request)
    {
        try {
            $get = $request->all();
            $info['pushcallback'] = json_encode($get);
            tel::where('orderno', $get['downstreamSerialno'])->update($info);
            $status = "success";
            switch ($get['status']) {
                case 2:
                    $order = tel::where('orderno', $get['downstreamSerialno'])->first();
                    $time = time();
                    $oktime = $time - $order['time'];
                    $info = ['pid' => 22, 'branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                    tel::where("orderno", $get['downstreamSerialno'])->update($info);
                    $data = [
                        "order" =>  $order['order'],
                        "orderno" =>  $order['orderno'],
                        "chargeaccount" =>  $order['chargeaccount'],
                        "order_status" =>  1,
                        "finish_time" =>  time(),
                    ];
                    curlPost($order['callbackUrl'],json_encode($data));
                    $res = Branch::where("id",15)->select("balance")->first();
                    $info['balance'] = $res->balance - $order['price'];
                    Branch::where("id",15)->update($info);
                    $status = "success";
                    break;
                case 3:
                    /**********直接回调失败*********/
                    $order = tel::where('orderno', $get['downstreamSerialno'])->first();
//                    $this->callBackOrderStatus($get['tradeNoThird'], 2, time());
                    $infoFulu['branch'] = "充值失败";
                    $infoFulu['status'] = '已处理';
                    $infoFulu['isok'] = 2;
                    $infoFulu['pid'] = 22;
                    $infoFulu['oktime'] = time() - $order['time'];
                    tel::where('orderno', $order['orderno'])->update($infoFulu);
                    $data = [
                        "order" =>  $order['order'],
                        "orderno" =>  $order['orderno'],
                        "chargeaccount" =>  $order['chargeaccount'],
                        "order_status" =>  2,
                        "finish_time" =>  time(),
                    ];
                    curlPost($order['callbackUrl'],json_encode($data));
                    break;
                default:
                    $order = tel::where('orderno', $get['downstreamSerialno'])->first();
//                    $this->callBackOrderStatus($get['tradeNoThird'], 2, time());
                    $infoFulu['branch'] = "充值失败";
                    $infoFulu['status'] = '已处理';
                    $infoFulu['isok'] = 2;
                    $infoFulu['pid'] = 22;
                    tel::where('orderno', $order['orderno'])->update($infoFulu);
                    $data = [
                        "order" =>  $order['order'],
                        "orderno" =>  $order['orderno'],
                        "chargeaccount" =>  $order['chargeaccount'],
                        "order_status" =>  2,
                        "finish_time" =>  time(),
                    ];
                    curlPost($order['callbackUrl'],json_encode($data));
            }
            return $status;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    //五色石充值回调
    public function fiveAutoConfirmOrder(Request $request)
    {
        try {
            $get = $request->all();
            $info['buyerip'] = json_encode($get);
            PushOrder::where('orderno', $get['tradeNoThird'])->update($info);
//            Order1 ::where('orderno',$get['tradeNoThird'])->update($info);
            $status = "success";
            switch ($get['code']) {
                case 200:
                    $order = PushOrder::where('orderno', $get['tradeNoThird'])->first();
//                    $order = Order1::where('orderno',$get['tradeNoThird'])->first();
                    $time = time();
                    $oktime = $time - $order['time'];
                    $info = ['bid' => 15, 'branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                    PushOrder::where("orderno", $get['tradeNoThird'])->update($info);
//                    Order1::where("orderno",$get['tradeNoThird'])->update($info);
                    if ($order['pid'] == 0) {
                        Order::where("orderno", $get['tradeNoThird'])->update($info);
                    }
                    $this->callBackOrderStatus($get['tradeNoThird'], 1, time());
                    $this->checkOrderStatus($order['pid'], $get['tradeNoThird'], 1);
                    $status = "success";
                    $branch = Branch::where("id", 15)->first();
                    $order1['money'] = $branch['money'] + $order->remainingnumber;
                    $order1['successmoney'] = $branch['successmoney'] + $order->remainingnumber;
                    $order1['successnum'] = $branch['successnum'] + 1;
                    Branch::where("id", 15)->update($order1);
                    break;
                case 300:
                    /**********测试start*********/
//                    $order = Order1::where('orderno',$get['tradeNoThird'])->first();
//                    $time = time();
//                    $oktime = $time - $order['time'];
//                    $info = ['bid' => 15, 'branch' => '充值失败', 'isok' => 2, 'oktime' => $oktime];
//                    Order1::where("orderno",$get['tradeNoThird'])->update($info);
//                    return "success";
//                    die;
                    /**********测试end*********/
                    /**********直接回调失败*********/
                    $order = Order::where('orderno', $get['tradeNoThird'])->first();
                    $this->callBackOrderStatus($get['tradeNoThird'], 2, time());
                    $infoFulu['branch'] = "充值失败";
                    $infoFulu['status'] = '已处理';
                    $infoFulu['isok'] = 2;
                    $infoFulu['bid'] = 15;
                    Order::where('orderno', $order['orderno'])->update($infoFulu);
                    PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
                    /**********直接回调失败end*********/
                    /**********福禄直充start*********/
//                    $order = PushOrder::where('orderno', $get['tradeNoThird'])->first();
//                    switch ($order['remainingnumber']) {
//                        case 50:
//                            $product_id = 500060707;
//                            break;
//                        case 100:
//                            $product_id = 500060708;
//                            break;
//                        case 200:
//                            $product_id = 500060709;
//                            break;
//                        case 300:
//                            $product_id = 500060710;
//                            break;
//                        case 500:
//                            $product_id = 500060712;
//                            break;
//                        case 1000:
//                            $product_id = 500060713;
//                            break;
//                    }
//                    $resFulu = $this->pushOrderFulu($product_id, $order['orderno'], $order['chargeaccount']);
//                    if ($resFulu->code == 0) {
//                        $infoFulu['branch'] = '已推送快充';
//                        $infoFulu['status'] = '已处理';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    } else {
//                        $infoFulu['branch'] = $resFulu->code;
//                        $infoFulu['status'] = '查询福禄失败原因';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    }
                    /**********福禄直充end*********/
                    break;
                default:
                    $info = ['branch' => '超时'];
            }
            return $status;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    //放羊娃充值回调
    public function fywAutoConfirmOrder(Request $request)
    {
        try {
            $get = $request->all();
            $info['buyerip'] = json_encode($get);
            PushOrder::where('orderno', $get['mchOrderNo'])->update($info);
//            Order1 ::where('orderno',$get['mchOrderNo'])->update($info);
            $status = "success";
            switch ($get['directStatus']) {
                case 5:
                    $order = PushOrder::where('orderno', $get['mchOrderNo'])->first();
                    $time = time();
                    $oktime = $time - $order['time'];
                    $info = ['bid' => 8, 'branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                    PushOrder::where("orderno", $get['mchOrderNo'])->update($info);
                    if ($order['pid'] == 0) {
                        Order::where("orderno", $get['mchOrderNo'])->update($info);
                    }
                    $this->callBackOrderStatus($get['mchOrderNo'], 1, time());
                    $this->checkOrderStatus($order['pid'], $get['mchOrderNo'], 1);
                    $status = "success";
                    $branch = Branch::where("id", 8)->first();
                    $order1['money'] = $branch['money'] + $order->remainingnumber;
                    $order1['successmoney'] = $branch['successmoney'] + $order->remainingnumber;
                    $order1['successnum'] = $branch['successnum'] + 1;
                    Branch::where("id", 8)->update($order1);
                    break;
                case 3:
                    /**********测试start*********/
//                    $order = Order1::where('orderno',$get['mchOrderNo'])->first();
//                    $time = time();
//                    $oktime = $time - $order['time'];
//                    $info = ['bid' => 15, 'branch' => '充值失败', 'isok' => 2, 'oktime' => $oktime];
//                    Order1::where("orderno",$get['mchOrderNo'])->update($info);
//                    return "success";
//                    die;
                    /**********测试end*********/
                    /**********直接回调失败*********/
                    $order = Order::where('orderno', $get['mchOrderNo'])->first();
                    $this->callBackOrderStatus($get['mchOrderNo'], 2, time());
                    $infoFulu['branch'] = "充值失败";
                    $infoFulu['status'] = '已处理';
                    $infoFulu['isok'] = 2;
                    $infoFulu['bid'] = 8;
                    $infoFulu['oktime'] = time() - $order['time'];
                    Order::where('orderno', $order['orderno'])->update($infoFulu);
                    PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
                    /**********直接回调失败end*********/
                    /**********福禄直充start*********/
//                    $order = PushOrder::where('orderno', $get['tradeNoThird'])->first();
//                    switch ($order['remainingnumber']) {
//                        case 50:
//                            $product_id = 500060707;
//                            break;
//                        case 100:
//                            $product_id = 500060708;
//                            break;
//                        case 200:
//                            $product_id = 500060709;
//                            break;
//                        case 300:
//                            $product_id = 500060710;
//                            break;
//                        case 500:
//                            $product_id = 500060712;
//                            break;
//                        case 1000:
//                            $product_id = 500060713;
//                            break;
//                    }
//                    $resFulu = $this->pushOrderFulu($product_id, $order['orderno'], $order['chargeaccount']);
//                    if ($resFulu->code == 0) {
//                        $infoFulu['branch'] = '已推送快充';
//                        $infoFulu['status'] = '已处理';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    } else {
//                        $infoFulu['branch'] = $resFulu->code;
//                        $infoFulu['status'] = '查询福禄失败原因';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    }
                    /**********福禄直充end*********/
                    break;
                case 2:
                    /**********测试start*********/
//                    $order = Order1::where('orderno',$get['mchOrderNo'])->first();
//                    $time = time();
//                    $oktime = $time - $order['time'];
//                    $info = ['bid' => 15, 'branch' => '充值失败', 'isok' => 2, 'oktime' => $oktime];
//                    Order1::where("orderno",$get['mchOrderNo'])->update($info);
//                    return "success";
//                    die;
                    /**********测试end*********/
                    /**********直接回调失败*********/
                    $order = Order::where('orderno', $get['mchOrderNo'])->first();
                    $this->callBackOrderStatus($get['mchOrderNo'], 2, time());
                    $infoFulu['branch'] = "充值失败";
                    $infoFulu['status'] = '已处理';
                    $infoFulu['isok'] = 2;
                    $infoFulu['bid'] = 8;
                    $infoFulu['oktime'] = time() - $order['time'];
                    Order::where('orderno', $order['orderno'])->update($infoFulu);
                    PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
                    /**********直接回调失败end*********/
                    /**********福禄直充start*********/
//                    $order = PushOrder::where('orderno', $get['tradeNoThird'])->first();
//                    switch ($order['remainingnumber']) {
//                        case 50:
//                            $product_id = 500060707;
//                            break;
//                        case 100:
//                            $product_id = 500060708;
//                            break;
//                        case 200:
//                            $product_id = 500060709;
//                            break;
//                        case 300:
//                            $product_id = 500060710;
//                            break;
//                        case 500:
//                            $product_id = 500060712;
//                            break;
//                        case 1000:
//                            $product_id = 500060713;
//                            break;
//                    }
//                    $resFulu = $this->pushOrderFulu($product_id, $order['orderno'], $order['chargeaccount']);
//                    if ($resFulu->code == 0) {
//                        $infoFulu['branch'] = '已推送快充';
//                        $infoFulu['status'] = '已处理';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    } else {
//                        $infoFulu['branch'] = $resFulu->code;
//                        $infoFulu['status'] = '查询福禄失败原因';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    }
                    /**********福禄直充end*********/
                    break;
                default:
                    $info = ['branch' => '超时'];
            }
            return $status;
        } catch (\Exception $e) {
            echo $e->getMessage();
            echo $e->getLine();
        }
    }

    //皓丰充值回调
    public function hfAutoConfirmOrder(Request $request)
    {
        try {
            $get = $request->all();
            $info['buyerip'] = json_encode($get);
            PushOrder::where('orderno', $get['tradeNoThird'])->update($info);
//            Order1 ::where('orderno',$get['tradeNoThird'])->update($info);
            $status = "success";
            switch ($get['code']) {
                case 200:
                    $order = PushOrder::where('orderno', $get['tradeNoThird'])->first();
//                    $order = Order1::where('orderno',$get['tradeNoThird'])->first();
                    $time = time();
                    $oktime = $time - $order['time'];
                    $info = ['bid' => 17, 'branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                    PushOrder::where("orderno", $get['tradeNoThird'])->update($info);
//                    Order1::where("orderno",$get['tradeNoThird'])->update($info);
                    if ($order['pid'] == 0) {
                        Order::where("orderno", $get['tradeNoThird'])->update($info);
                    }
                    $this->callBackOrderStatus($get['tradeNoThird'], 1, time());
                    $this->checkOrderStatus($order['pid'], $get['tradeNoThird'], 1);
                    $status = "success";
                    $branch = Branch::where("id", 15)->first();
                    $order1['money'] = $branch['money'] + $order->remainingnumber;
                    $order1['successmoney'] = $branch['successmoney'] + $order->remainingnumber;
                    $order1['successnum'] = $branch['successnum'] + 1;
                    Branch::where("id", 17)->update($order1);
                    break;
                case 300:
                    /**********测试start*********/
//                    $order = Order1::where('orderno',$get['tradeNoThird'])->first();
//                    $time = time();
//                    $oktime = $time - $order['time'];
//                    $info = ['bid' => 17, 'branch' => '充值失败', 'isok' => 2, 'oktime' => $oktime];
//                    Order1::where("orderno",$get['tradeNoThird'])->update($info);
//                    return "success";
//                    die;
                    /**********测试end*********/
                    $order = PushOrder::where('orderno', $get['tradeNoThird'])->first();
                    switch ($order['remainingnumber']) {
                        case 100:
                            $product_id = 10707887;
                            break;
                        case 200:
                            $product_id = 19016689;
                            break;
                        case 300:
                            $product_id = 19016698;
                            break;
                        case 500:
                            $product_id = 19016699;
                            break;
                        case 1000:
                            $product_id = 19016700;
                            break;
                    }
                    $resFulu = $this->pushOrderFulu($product_id, $order['orderno'], $order['chargeaccount']);
                    if ($resFulu->code == 0) {
                        $infoFulu['branch'] = '已推送快充';
                        $infoFulu['status'] = '已处理';
                        $infoFulu['isok'] = 4;
                        $infoFulu['bid'] = 888;
                        Order::where('orderno', $order['orderno'])->update($infoFulu);
                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
                    } else {
                        $infoFulu['branch'] = $resFulu->code;
                        $infoFulu['status'] = '查询福禄失败原因';
                        $infoFulu['isok'] = 4;
                        $infoFulu['bid'] = 0;
                        Order::where('orderno', $order['orderno'])->update($infoFulu);
                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
                    }
                    break;
                default:
                    $info = ['branch' => '超时'];
            }
            return $status;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    //软游斗币充值回调
    public function ry_AutoConfirmOrder(Request $request)
    {
        try {
            $get = $request->all();
            $info['buyerip'] = json_encode($get);
            PushOrder::where('orderno', $get['orderId'])->update($info);
//            Order1 ::where('orderno',$get['orderId'])->update($info);
            $status = "success";
            switch ($get['code']) {
                case 1:
                    $info = ['branch' => '充值中'];
                    PushOrder::where("orderno", $get['orderId'])->update($info);
//                    Order1::where("orderno",$get['orderId'])->update($info);
                    break;
                case 2:
                    $order = PushOrder::where('orderno', $get['orderId'])->first();
//                    $order = Order1::where('orderno',$get['orderId'])->first();
                    $time = time();
                    $oktime = $time - $order['time'];
                    $info = ['bid' => 12, 'branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                    PushOrder::where("orderno", $get['orderId'])->update($info);
//                    Order1::where("orderno",$get['orderId'])->update($info);
                    if ($order['pid'] == 0) {
                        Order::where("orderno", $get['orderId'])->update($info);
                    }
                    $this->callBackOrderStatus($get['orderId'], 1, time());
                    $this->checkOrderStatus($order['pid'], $get['orderId'], 1);
                    $status = "success";
                    $branch = Branch::where("id", 12)->first();
                    $order1['money'] = $branch['money'] + $order->remainingnumber;
                    $order1['successmoney'] = $branch['successmoney'] + $order->remainingnumber;
                    $order1['successnum'] = $branch['successnum'] + 1;
                    Branch::where("id", 12)->update($order1);
                    break;
                case 3:
                    $order = Order::where('orderno', $get['orderId'])->first();
                    $this->callBackOrderStatus($get['orderId'], 2, time());
                    /*直接回调失败*/
                    $infoFulu['branch'] = "充值失败";
                    $infoFulu['status'] = '已处理';
                    $infoFulu['isok'] = 2;
                    $infoFulu['bid'] = 12;
                    Order::where('orderno', $order['orderno'])->update($infoFulu);
                    PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
                    /*直接回调失败end*/
//                    switch ($order['price']) {
//                case 50:
//                    $product_id = 500060707;
//                    break;
//                case 100:
//                    $product_id = 500060708;
//                    break;
//                case 200:
//                    $product_id = 500060709;
//                    break;
//                case 300:
//                    $product_id = 500060710;
//                    break;
//                case 500:
//                    $product_id = 500060712;
//                    break;
//                case 1000:
//                    $product_id = 500060713;
//                    break;
//                    }
//                    $resFulu = $this->pushOrderFulu($product_id, $order['orderno'], $order['chargeaccount'], $order['num']);
//                    if ($resFulu->code == 0) {
//                        $infoFulu['branch'] = '已推送快充';
//                        $infoFulu['status'] = '已处理';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    } else {
//                        $infoFulu['branch'] = $resFulu->code;
//                        $infoFulu['status'] = '查询福禄失败原因';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 0;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    }
                    break;
                default:
                    $info = ['branch' => '超时'];
            }
            return $status;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    //兴恒斗币充值回调
    public function xh_AutoConfirmOrder(Request $request)
    {
        try {
            $callback = $request->getContent();



            $info['buyerip'] = $callback;

            $data = json_decode($callback);



            $data = json_decode($data->orders,JSON_FORCE_OBJECT);



            $get['orderid'] = $data[0]['orderid'];



            PushOrder::where('orderno', $get['orderid'])->update($info);
            Order::where('orderno', $get['orderid'])->update($info);
//            Order1 ::where('id',1)->update($info);
            $status = "OK";


            if($data[0]['result'] == 'success'){
                $order = PushOrder::where('orderno', $get['orderid'])->first();
//                    $order = Order1::where('orderno',$get['orderId'])->first();
                $time = time();
                $oktime = $time - $order['time'];
                $info = ['bid' => 21, 'branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                PushOrder::where("orderno", $get['orderid'])->update($info);
//                    Order1::where("orderno",$get['orderId'])->update($info);
                if ($order['pid'] == 0) {
                    Order::where("orderno", $get['orderid'])->update($info);
                }
                $this->callBackOrderStatus($get['orderid'], 1, time());
                $this->checkOrderStatus($order['pid'], $get['orderid'], 1);
                $status = "OK";
                $branch = Branch::where("id", 21)->first();
                $order1['money'] = $branch['money'] + $order->remainingnumber;
                $order1['successmoney'] = $branch['successmoney'] + $order->remainingnumber;
                $order1['successnum'] = $branch['successnum'] + 1;
                Branch::where("id", 21)->update($order1);
            }else{
                $order = Order::where('orderno', $get['orderid'])->first();
                $this->callBackOrderStatus($get['orderid'], 2, time());
                /*直接回调失败*/
                $infoFulu['branch'] = "充值失败";
                $infoFulu['status'] = '已处理';
                $infoFulu['isok'] = 2;
                $infoFulu['bid'] = 21;
                $infoFulu['oktime'] =time() - $order['time'];
                Order::where('orderno', $order['orderno'])->update($infoFulu);
                PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
                /*直接回调失败end*/
//                    switch ($order['price']) {
//                case 50:
//                    $product_id = 500060707;
//                    break;
//                case 100:
//                    $product_id = 500060708;
//                    break;
//                case 200:
//                    $product_id = 500060709;
//                    break;
//                case 300:
//                    $product_id = 500060710;
//                    break;
//                case 500:
//                    $product_id = 500060712;
//                    break;
//                case 1000:
//                    $product_id = 500060713;
//                    break;
//                    }
//                    $resFulu = $this->pushOrderFulu($product_id, $order['orderno'], $order['chargeaccount'], $order['num']);
//                    if ($resFulu->code == 0) {
//                        $infoFulu['branch'] = '已推送快充';
//                        $infoFulu['status'] = '已处理';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 888;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    } else {
//                        $infoFulu['branch'] = $resFulu->code;
//                        $infoFulu['status'] = '查询福禄失败原因';
//                        $infoFulu['isok'] = 4;
//                        $infoFulu['bid'] = 0;
//                        Order::where('orderno', $order['orderno'])->update($infoFulu);
//                        PushOrder::where('orderno', $order['orderno'])->update($infoFulu);
//                    }
            }
            return $status;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    //软游话费充值回调
    public function ry_AutoConfirmHfOrder(Request $request)
    {
        try {
            $get = $request->all();
            $info['pushcallback'] = json_encode($get);
            tel::where('order', $get['orderId'])->update($info);
            $status = "success";
            switch ($get['code']) {
                case 1:
                    $info = ['branch' => '充值中'];
                    tel::where("order", $get['orderId'])->update($info);
                    break;
                case 2:
                    $order = tel::where('order', $get['orderId'])->first();
                    $time = time();
                    $oktime = $time - $order['time'];
                    $info = ['pid' => 12, 'branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                    tel::where("order", $get['orderId'])->update($info);
                    break;
                case 3:
                    $infoFulu['branch'] = "充值失败";
                    $infoFulu['status'] = '已处理';
                    $infoFulu['isok'] = 2;
                    $infoFulu['pid'] = 12;
                    $order = tel::where('order', $get['orderId'])->first();
                    $oktime = time() - $order['time'];
                    $infoFulu['oktime'] = $oktime;
                    tel::where("order", $get['orderId'])->update($infoFulu);
                    break;
                default:
                    $info = ['branch' => '超时'];
            }
            return $status;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    //手动查询福禄订单完成情况
    public function getOrderInfo(Request $request)
    {
        $product_id = $request->get('id');
        $table = $request->get('table');
        $param = [
            "customer_order_no" => $product_id
        ];
        $data = [
            "app_key" => $this->app_key,
            "method" => "fulu.order.info.get",
            "timestamp" => date("Y-m-d H:i:s", time()),
            "version" => "2.0",
            "format" => "json",
            "charset" => "utf-8",
            "sign_type" => "md5",
            "app_auth_token" => "",
            "biz_content" => json_encode($param),
        ];
        $sign = $this->getSign($data);

        $data['sign'] = $sign;

        $res = $this->curlFulu($this->url, json_encode($data, JSON_UNESCAPED_SLASHES));

        $res = json_decode($res);

        $result = json_decode($res->result);

        if ($table == 'PushOrder') {
            $orderInfo = PushOrder::where('orderno', $result->customer_order_no)->first();
            $oktime = time() - $orderInfo['buytime'];
            if ($result->order_state == "success") {
                $info = ['branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime,'ext' => json_encode($result)];
                PushOrder::where("orderno", $result->customer_order_no)->update($info);
                if ($orderInfo['pid'] == 0) {
                    Order::where("orderno", $result->customer_order_no)->update($info);
                    $this->callBackOrderStatus($result->customer_order_no, 1, time());
                } else {
                    $this->checkOrderStatus($orderInfo['pid'], $result->customer_order_no, 1);
                }
            } elseif ($result->order_state == "processing") {
                $info = ['branch' => '充值中', 'isok' => 4];
            } elseif ($result->order_state == "failed") {
                $info = ['status' => '已处理', 'branch' => '充值失败', 'isok' => 2, 'oktime' => $oktime, 'ext' => json_encode($result)];
                PushOrder::where("orderno", $result->customer_order_no)->update($info);
                if ($orderInfo['pid'] == 0) {
                    Order::where("orderno", $result->customer_order_no)->update($info);
                    $this->callBackOrderStatus($result->customer_order_no, 2, time());
                } else {
                    $this->checkOrderStatus($orderInfo['pid'], $result->customer_order_no, 2);
                }
            } else {
                $info = ['branch' => '未处理', 'isok' => 4, 'oktime' => $oktime, 'ext' => json_encode($result)];
            }
            PushOrder::where("orderno", $result->customer_order_no)->update($info);
        } else {
            $orderInfo = PushOrder::where('orderno', $result->customer_order_no)->first();
            $oktime = time() - $orderInfo['buytime'];
            if ($result->order_state == "success") {
                $this->callBackOrderStatus($result->customer_order_no, 1, time());
                $info = ['branch' => '充值成功', 'isok' => 1, 'oktime' => $oktime];
                Order::where("orderno", $result->customer_order_no)->update($info);
            } elseif ($result->order_state == "processing") {
                $info = ['branch' => '充值中', 'isok' => 4];
            } elseif ($result->order_state == "failed") {
                $this->callBackOrderStatus($result->customer_order_no, 2, time());
                $info = ['status' => '已处理', 'branch' => '充值失败', 'isok' => 2, 'oktime' => $oktime, 'ext' => json_encode($result)];
                Order::where("orderno", $result->customer_order_no)->update($info);
            } else {
                $info = ['branch' => '未处理', 'isok' => 4, 'oktime' => $oktime, 'ext' => json_encode($result)];
            }
            Order::where("orderno", $result->customer_order_no)->update($info);
        }
    }

    function callBackOrderStatus($orderId, $orderStatus, $time, $str = 'Serge#57$768rtv0')
    {

        $sign = strtoupper(md5($orderId . $orderStatus . $time . $str));

        if ($orderStatus == 1) {
            $postData = "OrderId=" . $orderId . "&OrderStatus=" . $orderStatus . "&Description=充值成功&RealCost=0&Currency=RMB&SubstituteAccount=外部商户042&ChannelAccountId=外部商户042&Ts=" . $time . "&Sign=" . $sign;
        } else {
            $postData = "OrderId=" . $orderId . "&OrderStatus=" . $orderStatus . "&Description=充值失败&RealCost=0&Currency=RMB&SubstituteAccount=外部商户042&ChannelAccountId=外部商户042&Ts=" . $time . "&Sign=" . $sign;
        }


        $url = "http://interface.qsios.cn:8136/actionapi/Order/CallBackOrderStatus";

        $res = $this->curlPost($url, $postData);

        return $res;
    }

    public function curlPost($url, $post_data = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'charset=utf-8'));
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    //下单到福禄
    public function pushOrderFulu($product_id, $customer_order_no, $charge_account, $buy_num = 1)
    {
        $param = [
            "product_id" => $product_id,
            "customer_order_no" => $customer_order_no,
            "charge_account" => $charge_account,
            "buy_num" => $buy_num
        ];

        $data = [
            "app_key" => $this->app_key,
            "method" => "fulu.order.direct.add",
            "timestamp" => date("Y-m-d H:i:s", time()),
            "version" => "2.0",
            "format" => "json",
            "charset" => "utf-8",
            "sign_type" => "md5",
            "app_auth_token" => "",
            "biz_content" => json_encode($param),
        ];
        $sign = $this->getSign($data);

        $data['sign'] = $sign;

        $res = $this->curlFulu($this->url, json_encode($data, JSON_UNESCAPED_SLASHES));

        $res = json_decode($res);

        return $res;

    }

    public function curlFulu($url, $post_data = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function getSign($Parameters)
    {
        //签名步骤一：把字典json序列化
        $json = json_encode($Parameters, JSON_UNESCAPED_SLASHES);
        //签名步骤二：转化为数组
        $jsonArr = $this->mb_str_split($json);
        //签名步骤三：排序
        sort($jsonArr);
        //签名步骤四：转化为字符串
        $string = implode('', $jsonArr);
        //签名步骤五：在string后加入secret
        $string = $string . $this->app_secret;
        //签名步骤六：MD5加密
        $result_ = strtolower(md5($string));
        return $result_;
    }

    function mb_str_split($str)
    {
        return preg_split('/(?<!^)(?!$)/u', $str);
    }

    //检查订单状态是否全部完成
    public function checkOrderStatus($pid, $orderno, $status)
    {
        if ($pid == 0) {
            if ($status == 1) {
                $info = ['status' => '已处理', 'branch' => '充值成功', 'isok' => 1];
            } else {
                $info = ['status' => '已处理', 'branch' => '充值失败', 'isok' => 2];
            }
            $this->callBackOrderStatus($orderno, $status, time());
            Order::where('orderno', $orderno)->update($info);

        } else {
            $order = Order::where("id", $pid)->first();
            $price = $order['remainingnumber'];
            $money = intval(PushOrder::where(['pid' => $pid, 'isok' => 1])->sum('remainingnumber'));
            $failmoney = intval(PushOrder::where(['pid' => $pid, 'isok' => 2])->sum('remainingnumber'));
            if ($money == $price) {
                $info = ['bid' => 888, 'status' => '已处理', 'branch' => '充值成功', 'isok' => 1];
                Order::where('orderno', $order['orderno'])->update($info);
                OrderError::where('orderno', $order['orderno'])->delete();
                $this->callBackOrderStatus($order['orderno'], 1, time());
            } elseif ($failmoney == $price) {
                $info = ['bid' => 888, 'status' => '已处理', 'branch' => '充值失败', 'isok' => 2];
                Order::where('orderno', $order['orderno'])->update($info);
                OrderError::where('orderno', $order['orderno'])->delete();
                $this->callBackOrderStatus($order['orderno'], 2, time());
            } elseif ($price == $money + $failmoney) {
                if ($price <= 100) {
                    $info = ['bid' => 888, 'status' => '已处理', 'branch' => '充值失败', 'isok' => 2];
                    Order::where('orderno', $order['orderno'])->update($info);
                    $this->callBackOrderStatus($order['orderno'], 2, time());
                } else {
                    $info = ['bid' => 888, 'status' => '已处理', 'branch' => '充值成功', 'isok' => 1];
                    Order::where('orderno', $order['orderno'])->update($info);
                    $this->callBackOrderStatus($order['orderno'], 1, time());
                }
            } else {
                $info = ['status' => '已处理', 'branch' => '正在拆分充值', 'isok' => 5];
                Order::where('orderno', $order['orderno'])->update($info);
//                $info = ['orderno'=>$order['orderno'],'bid'=>$order['id'],'time'=>time()];
                $order['time'] = time();
                $res = OrderError::where("orderno", $order['orderno'])->first();
                if ($res == null) {
                    OrderError::where('orderno', $order['orderno'])->create($order);
                }
            }
        }
    }


}
